﻿using Framework;
using System;
using System.Threading.Tasks;

namespace WebHostApp
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebHostBuilder webHostBuilder = CreateWebHostBuilder(args);
            IWebHost webHost = webHostBuilder.Build();
            webHost.Run();

            Console.ReadKey();
        }


        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            IWebHostBuilder webHostBuilder = new WebHostBuilder();
            webHostBuilder.UseHttpListener();
            Action<IApplicationBuilder> configure = new Action<IApplicationBuilder>(InitApplication);
            return webHostBuilder.Configure(configure);
        }

        private static void InitApplication(IApplicationBuilder builder)
        {
            builder.Use(FooMiddleware)
             .Use(BarMiddleware)
             .Use(SeaMiddleware)
             .Use(KeyMiddleware)
             .Use(EndMiddleware);
        }

        #region 自定义中间件

        public static RequestDelegate FooMiddleware(RequestDelegate next)
        {
            return async delegate (HttpContext context)
            {
                string html = @"
                <html lang='zh-CN'>
                <head>
                    <meta charset='UTF-8'>
                    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
                    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
                    <title>自定义ASP.NET 框架</title>
                    <style>
                        body {
                            text-align: left;
                            font-size: 26px;
                        }
                        .content {
                            margin: 50px auto;
                            padding: 10px 10px;
                            width: 800px;
                            min-height: 300px;
                            border: 3px solid blue;
                            color: red;
                        }
                        .message {
                            text-align: center;
                            padding: 20px;
                            font-family: '微软雅黑';
                            font-size: 30px;
                            font-weight: bold; 
                            color: #ffffff;
                            background-color: #1e9fff;
                         }
                    </style>
                </head>
                <body>
                    <div class='content'>
                        <div class='message'>ASP.NET 自定义中间件</div>
                        <ol>
                            <li>Foo</li>";
                await context.Response.WriteAsync(html);
                await next(context);
            };
        }

        public static RequestDelegate BarMiddleware(RequestDelegate next)
        {
            return async delegate (HttpContext context)
            {
                await context.Response.WriteAsync("<li>Bar</li>");
                await next(context);
            };
        }

        public static RequestDelegate SeaMiddleware(RequestDelegate next)
        {
            Program.DisplayClassSea sea = new Program.DisplayClassSea();
            sea.next = next;
            return new RequestDelegate(sea.SeaContext);
        }

        public static RequestDelegate KeyMiddleware(RequestDelegate next)
        {
            return delegate (HttpContext context)
            {
                context.Response.WriteAsync("<li>Key</li>").Wait();
                Task task = next(context);
                return task;
            };
        }

        public static RequestDelegate EndMiddleware(RequestDelegate next)
        {
            return delegate (HttpContext context)
            {
                string html = @"<li>End</li>
                        </ol>
                    </div>
                </body>
                </html>";
                return context.Response.WriteAsync(html);
            };
        }

        #endregion

        internal class DisplayClassSea
        {
            public RequestDelegate next;

            public Task SeaContext(HttpContext context)
            {
                context.Response.WriteAsync("<li>Sea</li>").Wait();
                return next(context);
            }
        }
    }
}
